package org.light.generator;

import org.light.domain.Domain;
import org.light.utils.SqlReflector;

public class MysqlDBDefinitionGenerator extends DBDefinitionGenerator{

	public String generateDBSql(boolean createDB,boolean commentOn) throws Exception{
		// set up the database;
		if (commentOn == false) {
			StringBuilder sb = new StringBuilder();
			if (createDB == true){
				sb.append("drop database if exists ").append(this.getDbName()).append(";\n");
				sb.append("create database ").append(this.getDbName()).append("/*!40100 COLLATE 'utf8_general_ci' */;\n");
				sb.append("use ").append(this.getDbName()).append(";\n\n");
			}
				
			for (int i=0; i < this.getDomains().size();i++){
				Domain domain = this.getDomains().get(i);
				sb.append(SqlReflector.generateTableDefinition(domain)).append("\n");
			}
			return sb.toString();
		} else {
			StringBuilder sb = new StringBuilder();
			if (createDB == true){
				sb.append("drop database if exists ").append(this.getDbName()).append(";\n");
				sb.append("create database ").append(this.getDbName()).append("/*!40100 COLLATE 'utf8_general_ci' */;\n");
				sb.append("use ").append(this.getDbName()).append(";\n\n");
			}
				
			for (int i=0; i < this.getDomains().size();i++){
				Domain domain = this.getDomains().get(i);
				sb.append(SqlReflector.generateTableDefinitionWithComment(domain)).append("\n");
			}
			return sb.toString();
		}
	}
	
	public String generateDBSql(boolean commentOn) throws Exception{
		return generateDBSql(false, commentOn);
	}

	@Override
	public String generateDropTableSqls(boolean createNew) throws Exception {
		StringBuilder sb = new StringBuilder();
		for (int i=0; i < this.getDomains().size();i++){
			Domain domain = this.getDomains().get(i);
			sb.append(SqlReflector.generateMariaDBDropTableStatement(domain)).append("\n");
		}
		sb.append("\n");
		return sb.toString();
	}
	
}
